package com.firefighting.maintenancer;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Base64;
import java.util.Base64.Decoder;
import java.util.Base64.Encoder;



public class Base64Test 
{
	public static String getUrl=null;
	public static String GetImaheStr()
	{
		String imgFile="D:\\360\\99";
		InputStream in=null;
		byte[] data=null;
		try
		{
		in=new FileInputStream(imgFile);
		data = new byte[in.available()];
        in.read(data);
 
		}
		catch (IOException e) 
        {
            e.printStackTrace();
        }
        //对字节数组Base64编码
		Encoder encoder = Base64.getEncoder();
        return encoder.encodeToString(data);
		
	}
	 //base64字符串转化成图片
    public static boolean GenerateImage(String imgStr,String imgName)
    {   //对字节数组字符串进行Base64解码并生成图片
        if (imgStr == null) //图像数据为空
            return false;
        Decoder decoder = Base64.getDecoder();
        try 
        {
            //Base64解码
            byte[] b = decoder.decode(imgStr);
            for(int i=0;i<b.length;++i)
            {
                if(b[i]<0)
                {//调整异常数据
                    b[i]+=256;
                }
            }
            //生成jpeg图片
            //String imgFilePath = "D:\\360CloudUI\\tupian\\"+imgName+".jpg";//新生成的图片
            getUrl = "D:\\360CloudUI\\tupian\\"+imgName+".jpg";//新生成的图片
            OutputStream out = new FileOutputStream(getUrl);    
            out.write(b);
            out.flush();
            out.close();
            return true;
        } 
        catch (Exception e) 
        {
            return false;
        }
    }


}
